home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: Standard question - pointer initialization
- Date: 8 Mar 1996 13:41:16 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4hq9hsINN998@keats.ugrad.cs.ubc.ca>
- References: <4hk9un$906@hammer.msfc.nasa.gov> <4hl6rr$nde@news.xs4all.nl> <313E6028.1C19@ix.netcom.com> <4hnpsl$g8c@hacgate2.hac.com>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <4hnpsl$g8c@hacgate2.hac.com>,
- Ron Collins <collins@thor.tu.hac.com> wrote:
- >Norman Bullen (nbullen@ix.netcom.com) wrote:
- >[snip]
- >
- >: And there is no point in initializing a pointer when its declared if you
- >: don't know at that point what an appropriate value might be. Use of a
- >: pointer that has been initialized with an inappropriate value can be just
- >: as bad for the program as using an uninitialize pointer.
- >
- >The popular convention (when initializing pointers) is to set them to
- >NULL. This is the appropriate value to use when checking for invalid
- >pointer usage.
-
- This is not a popular convention, but something that is demanded by the
- language standard. A static uninitialized pointer shall take on a value that
- corresponds to a null pointer, whatever that bit pattern is.
-
- Also note that NULL is just a macro that stands for the value zero (often
- accompanied by a cast to void *). In assigning default initialization values to
- static variables, the compiler couldn't care less that there is a pre-processor
- macro called NULL. NULL can never be anything other than zero in a
- standard-conforming implementation of the C language.
-
- Under any C implementation, a constant integral expression having zero value
- produces a null pointer value in a context where a pointer is required,
- regardless of the physical representation of a null pointer required by the
- environment. Thus checking for a null pointer p by using ``if (!p)'' isn't any
- better or worse than ``if (p == NULL)'' (after inclusion of an appropriate
- standard header). Even on some imaginary computer where pointers are
- represented by some kind of associative look-up identifier, a standard C
- implementation would still ensure that the statement ``p = 0;'' turns p into a
- null pointer.
-
- By the way, null pointers are not for checking for incorrect pointer usage. It
- is obviously legal and useful for a pointer to have a null value. Such a value
- is guaranteed to always be distinct from a pointer which references an actual
- object.
- --
-
-